package com.ruoyi.project.system.job.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.course.domain.Course;
import com.ruoyi.project.system.course.mapper.CourseMapper;
import com.ruoyi.project.system.course.service.ICourseService;
import com.ruoyi.project.system.courseClass.domain.CourseClass;
import com.ruoyi.project.system.courseClass.mapper.CourseClassMapper;
import com.ruoyi.project.system.grades.domain.ClassGrades;
import com.ruoyi.project.system.grades.service.IClassGradesService;
import com.ruoyi.project.system.job.mapper.JobCourseClassMapper;
import com.ruoyi.project.system.submit.domain.JobSubmit;
import com.ruoyi.project.system.submit.mapper.JobSubmitMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.job.domain.JobCourseClass;
import com.ruoyi.project.system.job.service.IJobCourseClassService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 作业-课程-班级记录Controller
 *
 * @author ruoyi
 * @date 2021-04-15
 */
@Controller
@RequestMapping("/system/job")
public class JobCourseClassController extends BaseController
{
    private String prefix = "system/job";

    @Autowired
    private IJobCourseClassService jobCourseClassService;

    @Autowired
    private JobCourseClassMapper jobCourseClassMapper;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IClassGradesService classGradesService;

    @Autowired
    private JobSubmitMapper jobSubmitMapper;

    @Autowired
    private CourseClassMapper courseClassMapper;

    @RequiresPermissions("system:job:view")
    @GetMapping()
    public String job(ModelMap mmap)
    {
        String roleName = ShiroUtils.getSysUser().getRoleName();
        mmap.put("roleName",roleName);
        return prefix + "/job";
    }

    /**
     * 查询作业-课程-班级记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobCourseClass jobCourseClass)
    {
        startPage();
        String roleName = ShiroUtils.getSysUser().getRoleName();
        if("教师".equals(roleName)){
            jobCourseClass.setTeacherId(ShiroUtils.getUserId());
        }
        List<JobCourseClass> list = jobCourseClassService.selectJobCourseClassList(jobCourseClass);
        return getDataTable(list);
    }

    /**
     * 导出作业-课程-班级记录列表
     */
    @Log(title = "作业-课程-班级记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JobCourseClass jobCourseClass)
    {
        List<JobCourseClass> list = jobCourseClassService.selectJobCourseClassList(jobCourseClass);
        ExcelUtil<JobCourseClass> util = new ExcelUtil<JobCourseClass>(JobCourseClass.class);
        return util.exportExcel(list, "class");
    }

    /**
     * 新增作业-课程-班级记录
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("courseId",id);
        mmap.put("courseName",course.getName());
        // 获取所有已选择的班级信息
        List<ClassGrades> classGradesList = classGradesService.selectClassGradesList(new ClassGrades());
        List<ClassGrades> resultList = new ArrayList<>();
        CourseClass courseClassCondition = new CourseClass();
        courseClassCondition.setCourseId(id);
        List<CourseClass> courseClassList = courseClassMapper.selectCourseClassList(courseClassCondition);
        if(courseClassList.size() > 0 && classGradesList.size() > 0){
            for (CourseClass courseClass : courseClassList) {
                for (ClassGrades classGrades : classGradesList) {
                    if(courseClass.getClassId() == classGrades.getId()){
                        resultList.add(classGrades);
                    }
                }
            }
        }
        mmap.put("classGradesList",resultList);
        return prefix + "/add";
    }

    /**
     * 新增保存作业-课程-班级记录
     */
    @Log(title = "作业-课程-班级记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody JobCourseClass jobCourseClass)
    {
        return toAjax(jobCourseClassService.insertJobCourseClass(jobCourseClass));
    }

    /**
     * 修改作业-课程-班级记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long groupId, ModelMap mmap)
    {
        // 获取所有班级信息
        JobCourseClass condition = new JobCourseClass();
        condition.setGroupId(groupId);
        List<JobCourseClass> jobCourseClassList = jobCourseClassMapper.selectJobCourseClassList(condition);
        List<ClassGrades> classGradesList = classGradesService.selectClassGradesList(new ClassGrades());
        mmap.put("classGradesList",classGradesList);
        mmap.put("jobCourseClass", jobCourseClassList.get(0));
        // 查询所有题目
        if(jobCourseClassList.size() > 0){
            for (JobCourseClass courseClass : jobCourseClassList) {
                if(StringUtils.isNotEmpty(courseClass.getOptionJson())){
                    JSONObject jsonObject =JSON.parseObject(courseClass.getOptionJson());
                    courseClass.setOption(jsonObject.getString("option"));
                    courseClass.setOptionanswer(jsonObject.getString("optionanswer"));
                }
            }
        }
        mmap.put("jobCourseClassList", jobCourseClassList);
        return prefix + "/edit";
    }

    @GetMapping("/viewNoSubmitStudent/{id}")
    public String viewNoSubmitStudent(@PathVariable("id") Long id, ModelMap mmap)
    {
        List<JobCourseClass> jobCourseClassList = jobCourseClassService.selectJobCourseClassById(id);
        // 获取其下所有未提交作业的学生
        JobSubmit condition = new JobSubmit();
        condition.setJobId(jobCourseClassList.get(0).getGroupId());
        condition.setStatus(3);
        List<JobSubmit> jobSubmitList = jobSubmitMapper.selectJobSubmitList(condition);
        StringBuilder stringBuilder = new StringBuilder();
        if(jobSubmitList.size() > 0){
            for (JobSubmit jobSubmit : jobSubmitList) {
                stringBuilder.append(jobSubmit.getStudentName() + ",");
            }
        }
        if(StringUtils.isEmpty(stringBuilder.toString())){
            mmap.put("studentNames","所有学生均已提交作业");
        }else {
            mmap.put("studentNames",stringBuilder.toString());
        }
        return prefix + "/viewNoSubmitStudent";
    }

    /**
     * 修改保存作业-课程-班级记录
     */
    @Log(title = "作业-课程-班级记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody JobCourseClass jobCourseClass)
    {
        return toAjax(jobCourseClassService.updateJobCourseClass(jobCourseClass));
    }

    /**
     * 删除作业-课程-班级记录
     */
    @Log(title = "作业-课程-班级记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jobCourseClassService.deleteJobCourseClassByIds(ids));
    }
}
